package br.com.loja.view.swing;

import br.com.loja.model.entity.Switchable;
import br.com.loja.model.service.factory.ServiceFactory;
import br.com.loja.view.swing.model.EntityComboBoxModel;
import br.com.loja.view.swing.model.EntityDefaultTableModel;
import java.awt.Component;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.util.Calendar;
import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;

/**
 * @brief Classe EntityEditor
 * @author David Luiz
 * @date 14/12/2014
 */
public class ComponentFactoryOld extends AbstractComponentFactory {

    public ComponentFactoryOld(Field field, ServiceFactory serviceFactory) {
        super(field, serviceFactory);
    }

    @Override
    protected Component buildCompositionComponent(Field field, ServiceFactory serviceFactory) {

        Class<?> type = getClassType(field);

        if (isToMany(field)) {

            JTable table = new JTable(new EntityDefaultTableModel(type));
            table.setName(field.getName());

            return table;

        } else {

            JPanel panel = new JPanel();
            panel.setName(field.getName());
            PanelEntity panelEntity = new PanelEntity(serviceFactory);
            panel = panelEntity.getPanel((Class<? extends Switchable>) type);
            return panel;

        }

    }

    @Override
    protected Component buildAggregationComponent(Field field, ServiceFactory serviceFactory) {
        Class<?> type = getClassType(field);

        if (isToMany(field)) {
            DefaultListModel defaultListModel = new DefaultListModel();
            for (Object o : serviceFactory.getService(type).list()) {
                defaultListModel.addElement(o);
            }
            JList list = new JList(defaultListModel);
            list.setName(field.getName());

            return list;

        } else {
            JComboBox comboBox = new JComboBox(new EntityComboBoxModel(serviceFactory.getService(type).list()));
            comboBox.setName(field.getName());
            comboBox.setEditable(true);
            return comboBox;

        }
    }

    @Override
    protected Component buildEnumComponent(Field field, ServiceFactory serviceFactory) {

        Class<?> type = getClassType(field);

        if (isToMany(field)) {
            JList list = new JList<>(type.getEnumConstants());
            list.setName(field.getName());

            return list;

        } else {

            JComboBox comboBox = new JComboBox(type.getEnumConstants());
            comboBox.setName(field.getName());
            return comboBox;
        }
    }

    @Override
    protected Component buildCommonComponent(Field field, ServiceFactory serviceFactory) {
        Class<?> type = getClassType(field);

        if (isToMany(field)) {
            JList list = new JList();
            list.setName(field.getName());
            return list;
        } else if (Boolean.class.isAssignableFrom(type)) {
            JCheckBox checkBox = new JCheckBox();
            checkBox.setName(field.getName());
            return checkBox;

        } else if (Calendar.class.isAssignableFrom(type)) {
            try {
                JFormattedTextField formattedTextField;
                MaskFormatter maskFormatter = new MaskFormatter("##/##/####");
                maskFormatter.setValidCharacters("0123456789");
                formattedTextField = new JFormattedTextField(maskFormatter);
                formattedTextField.setName(field.getName());
                return formattedTextField;

            } catch (ParseException ex) {
                JTextField textField = new JTextField(25);
                textField.setName(field.getName());
                return textField;
            }

        } else {
            JTextField textField = new JTextField(25);
            textField.setName(field.getName());
            return textField;
        }
    }

}
